package common

import "strings"

// CardStatus 统一的卡状态常量
const (
	// 正常状态
	CardStatusNormal = "normal" // 可用

	// 待处理状态
	CardStatusPendingRecharge = "pending_recharge" // 待转入
	CardStatusUnactivated     = "unactivated"      // 未激活

	// 冻结状态
	CardStatusFreezing     = "freezing"      // 冻结中
	CardStatusFrozen       = "frozen"        // 冻结
	CardStatusRiskFrozen   = "risk_frozen"   // 风控冻结
	CardStatusSystemFrozen = "system_frozen" // 系统冻结
	CardStatusUnfreezing   = "unfreezing"    // 解冻中

	// 异常状态
	CardStatusExpired = "expired" // 过期

	// 注销状态
	CardStatusCanceling = "canceling" // 销卡中
	CardStatusCancelled = "cancelled" // 销卡

	// 其他状态
	CardStatusRenewing  = "renewing"  // 续卡中
	CardStatusReplacing = "replacing" // 补卡中
	CardStatusLost      = "lost"      // 挂失
	CardStatusStolen    = "stolen"    // 盗卡
	CardStatusPinLost   = "pin_lost"  // 密码锁定
)

// CardStatusMapping 平台状态到统一状态的映射
type CardStatusMapping struct {
	PhotonPay map[string]string
	HlPay     map[string]string
}

// GetCardStatusMapping 获取卡状态映射
func GetCardStatusMapping() *CardStatusMapping {
	return &CardStatusMapping{
		PhotonPay: map[string]string{
			// PhotonPay 状态映射
			"NORMAL":        CardStatusNormal,
			"ACTIVE":        CardStatusNormal,
			"INACTIVE":      CardStatusUnactivated,
			"FROZEN":        CardStatusFrozen,
			"FREEZING":      CardStatusFreezing,
			"UNFREEZING":    CardStatusUnfreezing,
			"EXPIRED":       CardStatusExpired,
			"CANCELLED":     CardStatusCancelled,
			"CANCELING":     CardStatusCanceling,
			"CLOSED":        CardStatusCancelled,
			"LOST":          CardStatusLost,
			"STOLEN":        CardStatusStolen,
			"PIN_LOST":      CardStatusPinLost,
			"RENEWING":      CardStatusRenewing,
			"REPLACING":     CardStatusReplacing,
			"RISK_FROZEN":   CardStatusRiskFrozen,
			"SYSTEM_FROZEN": CardStatusSystemFrozen,
		},
		HlPay: map[string]string{
			// HlPay 状态映射
			"ACTIVE":        CardStatusNormal,
			"NORMAL":        CardStatusNormal,
			"INACTIVE":      CardStatusUnactivated,
			"FROZEN":        CardStatusFrozen,
			"FREEZING":      CardStatusFreezing,
			"UNFREEZING":    CardStatusUnfreezing,
			"EXPIRED":       CardStatusExpired,
			"CANCELLED":     CardStatusCancelled,
			"CANCELING":     CardStatusCanceling,
			"CLOSED":        CardStatusCancelled,
			"LOST":          CardStatusLost,
			"STOLEN":        CardStatusStolen,
			"PIN_LOST":      CardStatusPinLost,
			"RENEWING":      CardStatusRenewing,
			"REPLACING":     CardStatusReplacing,
			"RISK_FROZEN":   CardStatusRiskFrozen,
			"SYSTEM_FROZEN": CardStatusSystemFrozen,
		},
	}
}

// MapPlatformStatusToUnified 将平台状态映射为统一状态
func MapPlatformStatusToUnified(platform string, platformStatus string) string {
	mapping := GetCardStatusMapping()

	// 转换为大写以进行匹配
	upperStatus := strings.ToUpper(platformStatus)

	switch platform {
	case "photonpay":
		if unifiedStatus, exists := mapping.PhotonPay[upperStatus]; exists {
			return unifiedStatus
		}
	case "hlpay":
		if unifiedStatus, exists := mapping.HlPay[upperStatus]; exists {
			return unifiedStatus
		}
	}

	// 如果没有找到映射，返回默认状态
	return CardStatusUnactivated
}

// MapUnifiedStatusToPlatform 将统一状态映射为平台状态
func MapUnifiedStatusToPlatform(platform string, unifiedStatus string) string {
	mapping := GetCardStatusMapping()

	// 反向映射
	switch platform {
	case "photonpay":
		for platformStatus, unified := range mapping.PhotonPay {
			if unified == unifiedStatus {
				return platformStatus
			}
		}
	case "hlpay":
		for platformStatus, unified := range mapping.HlPay {
			if unified == unifiedStatus {
				return platformStatus
			}
		}
	}

	// 如果没有找到映射，返回默认状态
	switch platform {
	case "photonpay":
		return "NORMAL"
	case "hlpay":
		return "ACTIVE"
	default:
		return "NORMAL"
	}
}

// IsValidCardStatus 检查卡状态是否有效
func IsValidCardStatus(status string) bool {
	validStatuses := []string{
		CardStatusNormal,
		CardStatusPendingRecharge,
		CardStatusUnactivated,
		CardStatusFreezing,
		CardStatusFrozen,
		CardStatusRiskFrozen,
		CardStatusSystemFrozen,
		CardStatusUnfreezing,
		CardStatusExpired,
		CardStatusCanceling,
		CardStatusCancelled,
		CardStatusRenewing,
		CardStatusReplacing,
		CardStatusLost,
		CardStatusStolen,
		CardStatusPinLost,
	}

	for _, validStatus := range validStatuses {
		if status == validStatus {
			return true
		}
	}
	return false
}

// GetCardStatusDescription 获取卡状态描述
func GetCardStatusDescription(status string) string {
	descriptions := map[string]string{
		CardStatusNormal:          "可用",
		CardStatusPendingRecharge: "待转入",
		CardStatusUnactivated:     "未激活",
		CardStatusFreezing:        "冻结中",
		CardStatusFrozen:          "冻结",
		CardStatusRiskFrozen:      "风控冻结",
		CardStatusSystemFrozen:    "系统冻结",
		CardStatusUnfreezing:      "解冻中",
		CardStatusExpired:         "过期",
		CardStatusCanceling:       "销卡中",
		CardStatusCancelled:       "销卡",
		CardStatusRenewing:        "续卡中",
		CardStatusReplacing:       "补卡中",
		CardStatusLost:            "挂失",
		CardStatusStolen:          "盗卡",
		CardStatusPinLost:         "密码锁定",
	}

	if description, exists := descriptions[status]; exists {
		return description
	}
	return "未知状态"
}

// IsCardActive 检查卡是否处于可用状态
func IsCardActive(status string) bool {
	return status == CardStatusNormal
}

// IsCardFrozen 检查卡是否处于冻结状态
func IsCardFrozen(status string) bool {
	frozenStatuses := []string{
		CardStatusFreezing,
		CardStatusFrozen,
		CardStatusRiskFrozen,
		CardStatusSystemFrozen,
	}

	for _, frozenStatus := range frozenStatuses {
		if status == frozenStatus {
			return true
		}
	}
	return false
}

// IsCardCancelled 检查卡是否已注销
func IsCardCancelled(status string) bool {
	return status == CardStatusCancelled || status == CardStatusCanceling
}
